const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const userRoutes = require('./routes/user');
const mpuserRoutes = require('./routes/mpuser');
const apiRoutes = require('./routes/api');
const expressLayouts = require('express-ejs-layouts');
const cors = require('cors');

const app = express();

// 使用cors中间件
app.use(cors());

// 设置模板引擎
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// 中间件
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressLayouts);

// 路由
app.use('/users', userRoutes);
app.use('/mpusers', mpuserRoutes);
app.use('/api', apiRoutes);

// 首页重定向到用户列表
app.get('/', (req, res) => {
  res.redirect('/users');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`);
}); 